const ConvModel = require('../model/conv.model')
const NewsModel = require('../model/news.model')
const UserModel = require('../model/user.model')
const RelationshipModel = require('../model/relationship.model')
const NotifyModel = require('../model/notify.model')
const { verifyToken } = require("../utils/jwt");
const logger = require('../utils/log4js')
const { verifyParams } = require('../utils/utils')
// const file = require('node:file')
const path = require('path')
const fs = require('fs')
module.exports = {
    upload: async (req, res) => {
        if (!req.files || Object.keys(req.files).length === 0) {
            return res.status(400).send('No files were uploaded.');
        }
        let sampleFile = req.files.image;
        let fileName = `${Date.now()}.png`
        let uploadPath = path.join(__dirname, '../public/img/', fileName)
        try {
            await sampleFile.mv(uploadPath)
        } catch (e) {
            return res.status(500).send(e);
        }
        // type 1：头像 2：其他
        const { type } = req.body
        console.log('type', type)
        let token = req.headers.token
        let { data } = verifyToken(token)

        if (type === '1') {
            try {
                await UserModel.findOneAndUpdate({
                    _id: data._id
                }, {
                    avatar: '/img/' + fileName
                })
            } catch (e) {
                return res.json({
                    code: 0,
                    msg: '头像上传失败'
                })
            }
        }
        return res.json({
            code: 1,
            msg: '上传成功',
            result: {
                avatar: '/img/' + fileName
            }
        })

    }
}